﻿using System.Web.Mvc;
using Kratos.Framework.ContentManagement.Models.Order;
using Kratos.Framework.Extensions;
using Kratos.Framework.Filters;
using Kratos.Framework.Modules.Core.Controllers;
using Kratos.Framework.Modules.Core.Services;
using Kratos.Modules.Order.Services;
using MvcContrib;

namespace Kratos.Modules.Order.Controllers
{
    public class OrderController : KratosController
    {
        private readonly IUserInfoServices _userService;
        private readonly IOrderService _orderService;

        public OrderController(IUserInfoServices userService, IOrderService orderService)
        {
            _userService = userService;
            _orderService = orderService;
        }

        [KratosSecurity, Ajax(HttpVerbs.Get)]
        public ActionResult Index()
        {
            var model = //CurrentUser.Orders;
                _userService.GetUser("zkwin123").Orders;
            var o = _orderService.GetById(1);
            return View(model);
        }

        [KratosSecurity]
        public ActionResult Create()
        {
            //.load('{0}')".With(url)
            string url = Url.Action<OrderController>(o => o.Index());
            string script = "$('#dialog').dialog({ resizable: false, modal: true, minWidth: 600 });";
            script += "$('#test').load('{0}')".With(url);
            return Content("<script>" + script + "</script>");
        }

        [HttpPost]
        [KratosSecurity]
        [UnitOfWork]
        public ActionResult Create(Orders order)
        {
            //order.User = CurrentUser;
            //_orderService.Save(order);
            CurrentUser.AddOrder(order);
            _userService.Save(CurrentUser);
            return this.RedirectToAction(c => Index());
        }
    }
}
